package com.qf.sl.youshare.tasks;

import android.os.AsyncTask;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.utils.HttpTools;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/3.
 * 20:36
 */

/**
 * 平台商品详情下载任务，需要传递 商品ID 参数
 */
public class GoodsTask extends AsyncTask<String,Integer,JSONObject> {

    private TaskProcessor processor;

    public GoodsTask(TaskProcessor processor) {
        this.processor = processor;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;

        if (params != null && params.length > 0) {
            String goodId = params[0];

            String url = Constants.GOODS_URL + goodId;

            byte[] bytes = HttpTools.get(url);

            try {
                String string = new String(bytes,"UTF-8");
                ret = new JSONObject(string);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (processor != null) {
            processor.processResult(jsonObject);
        }
    }
}
